<template>
	<!-- 自定义表单 -->
	<view class="main">
		<template v-if="init">
			<edit-form @goAgainSub="goAgainSub" ref="edit_view" v-if="detail.pk_id" :detail="detail" :formId="formId"
				@subFinsh="subFinsh"></edit-form>
			<add-form-view v-else :formId="formId" @resetLogin="resetLogin" @subFinsh="subFinsh" @initAdd="initAdd" :canSub="canSub">
			</add-form-view>
		</template>
		<sww-login></sww-login>
	</view>
</template>

<script>
	import addFormView from './pageComponents/addFormView.vue';
	import editForm from './pageComponents/editForm';
	import {
		pageShare
	} from '@/mixins/pageShare';
	export default {
		mixins: [pageShare],
		components: {
			'add-form-view': addFormView,
			editForm
		},
		data() {
			return {
				formId: null,
				init: false,
				showInit: false,
				detail: {}, // 提交过的资料
				canSub:true
			};
		},
		onLoad(options) {
			if (options && options.id) {
				this.formId = Number(options.id);
			}
			// this.init = true;
			// return
			this.$login('$initPage', {}, 'noLogin').then(() => {
				this.$initPage();
			});
		},
		onShow() {
			if (this.showInit && this.detail.pk_id) {
				this.$initPage('show');
			} else {
				this.showInit = true;
			}
		},
		onShareAppMessage: function() {
			return this.getShareAppMessage();
		},
		onShareTimeline: function() {
			return this.getShareTimeline();
		},
		methods: {
			// 记录列表页也会调用
			goAgainSub() {
				if(!this.canSub){
					let str = this.parameter.subTime.replace(',','-')
					this.$toast(`请在以下时间内提交：${str}`)
					return
				}
				this.detail = {}
			},
			// 查询是否提交过资料
			$initPage(type) {
				this.$loading();
				let params = {
					pk_id: this.formId
				};
				this.$resPost('GetCustomFormValInfo', params).then(res => {
					this.$hide();
					let parameter = JSON.parse(res.data.parameter || "{}")
					if (res.data.pk_id && parameter.form.showSub != 2) {
						// parameter.form.repeat_ill == 1?res.data.judeg = true : res.data.judeg = false // 判断是否可重复提交
						// if(parameter.form.repeat_ill != 1){
						uni.setNavigationBarTitle({
							title: this.vuex_business.pk_id == 1035 ? '提交成功' : (res.data.name || '预约查询')
						})
						// }
						// if(type == 'show'){
						// 	this.$nextTick(()=>{
						// 		this.$refs.edit_view.stateJudge()
						// 	})
						// }
					}
					this.detail = {
						...res.data,
						pk_id: parameter.form && parameter.form.showSub == 2 ? null : res.data.pk_id
					};

					this.setParameter(parameter)
					this.init = true;
				});
			},
			initAdd(obj) {
				if (!this.parameter.form) {
					this.setParameter(obj)
				}
			},
			setParameter(parameter) {
				let canSub = true
				if (parameter.subTime) {
					canSub = this.judgesubTime(parameter.subTime) // 判断是否在提交时间内
				}
				this.canSub = canSub
				this.parameter = {
					...parameter
				}
			},
			judgesubTime(subTime) {
				let objtime = new Date();
				let asletime = subTime.split(',');
				let currenttime = objtime.getHours() * 60 + objtime.getMinutes();
				let starttime = Number(asletime[0].split(':')[0]) * 60 + Number(asletime[0].split(':')[1]);
				let endttime = Number(asletime[1].split(':')[0]) * 60 + Number(asletime[1].split(':')[1]);
				if (starttime > currenttime || currenttime > endttime) {
					return false
				} else {
					return true
				}
			},
			// 暂不登录
			noLogin() {
				this.init = true;
			},
			resetLogin() {
				this.$initPage();
			},
			subFinsh(val) {
				if (this.parameter.form.showSub == 2) {
					this.uniBack()
				} else {
					if (val) {
						this.init = false
					}
					this.$initPage('show');
				}
			}
		}
	};
</script>
<style>
	page {
		background-color: #f5f5f5;
	}
</style>
